﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CNotebook
{
    public class ReportConfigManager:ConnectionManager
    {
        public SqlException UpdateReports(DataTable dt)
        {
            SqlCommand oCommand = new SqlCommand();
            try
            {
                
                foreach (DataRow oRow in dt.Rows)
                {
                    if (oRow.RowState == DataRowState.Modified)
                    {
                        oCommand = new SqlCommand();
                        oCommand.Connection = this.Connection;

                        oCommand.CommandText = "UPDATE ORPT SET ReportFile=@ReportFile WHERE DocType=@DocType";
                        oCommand.Parameters.Add(new SqlParameter("@DocType", oRow["DocType"]));
                        oCommand.Parameters.Add(new SqlParameter("@ReportFile", oRow["ReportFile"]));

                        oCommand.ExecuteNonQuery();
                    }
                }

                dt.AcceptChanges();
                return null; // No Errors
            }
            catch (SqlException ex)
            {
                return ex;
            }
        }

        public DataTable GetReports()
        {
            DataTable dt = new DataTable();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();

            oCommand.Connection = this.Connection;
            oCommand.CommandText = "SELECT * FROM ORPT";
            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            return dt;
        }

        public string GetReportFileName(int DocType)
        {
            DataTable dt = new DataTable();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();

            oCommand.Connection = this.Connection;
            oCommand.CommandText = "SELECT * FROM ORPT WHERE DocType=@DocType";
            oCommand.Parameters.Add(new SqlParameter("@DocType", DocType));

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            return dt.Rows[0]["ReportFile"].ToString().Trim();
        }
    }
}
